/**
 * <p>Copyright @ 2009 深圳市金绎科技发展有限公司 </p>
 * <p>All right reserved. </p>
 * <p>项目名称					： 东莞项目</p>
 * <p>JDK使用版本号				： jdk1.6.0_12 </P>
 * <p>描述						： 自定义标签,获得字典表的内容,</p>
 * @version						： 1.0.0 
 * @author						： created by 631wj 
 * 
 * <p>修改历史 </p>
 * 		
 * <p>修改时间     						修改人员             修改内容 </p>
 * <p> 2009-03-31                    	631wj				新建   </p>
 */
package com.pengtu.gsj.taglib;

import java.io.IOException;
import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;

import com.pengtu.gsj.entity.base.LabelValue;
import com.pengtu.gsj.utils.LabelHelper;

/**
 *  获得码表键值对的 标签
 * <p>
 * <b>NOTE</b> - This tag requires a Java2 (JDK 1.2 or later) platform.
 * </p>
 * 
 * @author Jens Fischer, Matt Raible
 * @version $Revision: 1.1 $ $Date: 2006-06-10 08:00:48 -0600 (Sat, 10 Jun
 *          2006) $
 * 
 * @jsp.tag name="country" bodycontent="empty"
 */
public class CodeTag extends TagSupport implements Serializable {
	private static final long serialVersionUID = 3905528206810167095L;
	private String name;
	private String prompt;
	private String scope;
	private String selected;
	private String code;
	private String multiple;
	private boolean hasEmpty =false;
	private int category;
	private int width;
	private boolean both =false;
	private boolean json =false;
	private boolean disabled = false;
	/**
	 * @param name
	 *            The name to set.
	 * 
	 * @jsp.attribute required="false" rtexprvalue="true"
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * 是否同时显示code 与编码
	 * @param both
	 */
	public void setBoth(boolean both) {
		this.both = both;
	}

	/**
	 * 字典表Code
	 * @param code
	 */
	public void setCode(String code) {
		this.code = code;
	}
	/**
	 * 字典表类型
	 * @param category
	 */
	public void setCategory(int category) {
		this.category = category;
	}
	
	public void setMultiple(String multiple) {
		this.multiple = multiple;
	}

	public boolean isJson() {
		return json;
	}
	public void setJson(boolean json) {
		this.json = json;
	}

	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}
	/**
	 * @param prompt
	 *            The prompt to set.
	 * @jsp.attribute required="false" rtexprvalue="true"
	 */
	public void setPrompt(String prompt) {
		this.prompt = prompt;
	}

	public boolean isHasEmpty() {
		return hasEmpty;
	}
	public void setHasEmpty(boolean hasEmpty) {
		this.hasEmpty = hasEmpty;
	}
	
	
	/**
	 * @param selected
	 *            The selected option.
	 * @jsp.attribute required="false" rtexprvalue="true"
	 */
	public void setDefault(String selected) {
		this.selected = selected;
	}

	/**
	 * Property used to simply stuff the list of codes into a specified
	 * scope.
	 * 
	 * @param scope
	 * 
	 * @jsp.attribute required="false" rtexprvalue="true"
	 */
	public void setToScope(String scope) {
		this.scope = scope;
	}

	/**
	 * Process the start of this tag.
	 * 
	 * @return int status
	 * 
	 * @exception JspException
	 *                if a JSP exception has occurred
	 * 
	 * @see javax.servlet.jsp.tagext.Tag#doStartTag()
	 */

	public int doStartTag() throws JspException {
		
		//如果code 不为空 那么 将显示该code编码对应的名称
		if(code != null){
			String codeName =LabelHelper.getInstance().buildCodeName(category,code,both);	
			try {
				pageContext.getOut().write(codeName);
			} catch (IOException e) {
				throw new JspException(e);
			}
		} 
		// 否则显示该 category类别的的一系列值 可能是select 也可能是check类型
		else{
			List<LabelValue> codes = LabelHelper.getInstance().buildCodeNames(category,both);	
		  if (scope != null) {
				if (scope.equals("page")) {
					pageContext.setAttribute(name, codes);
				} else if (scope.equals("request")) {
					pageContext.getRequest().setAttribute(name, codes);
				} else if (scope.equals("session")) {
					pageContext.getSession().setAttribute(name, codes);
				} else if (scope.equals("application")) {
					pageContext.getServletContext().setAttribute(name, codes);
				} else {
					throw new JspException(
							"Attribute 'scope' must be: page, request, session or application");
				}
			} else if(json) {
				StringBuilder sb = new StringBuilder();
				sb.append("[");
				for (Iterator<LabelValue> iterator = codes.iterator(); iterator.hasNext();) {
					LabelValue labelValue = iterator.next();
					sb.append("[");
					sb.append("'"+labelValue.getValue()+"','" +labelValue.getLabel()+"'");
					if(iterator.hasNext()){
						sb.append("],");
					} else{
						sb.append("]");
					}
				}
				 sb.append("]");
				try {
					pageContext.getOut().write(sb.toString());
				} catch (IOException io) {
					throw new JspException(io);
				}
			} else {
				StringBuilder sb = new StringBuilder();
				if(org.apache.commons.lang.StringUtils.isNotEmpty(multiple)){
					sb.append("<select   name=\"" + name + "\" id=\"" + name
							+ "\"  multiple=\"multiple\" class=\"select\" "+(width==0?"":"style=\"width: "+width+"px\"") +"  >\n");
				}else{
//					sb.append("<select   name=\"" + name + "\" id=\"" + name
//							+ "\" class=\"select\" "+(width==0?"":"style=\"width: "+width+"px\"") +"  >\n");
					sb.append("<select   name=\"" + name + "\" id=\"" + name+"\"");
					if (disabled) {
						sb.append(" disabled=disabled");
					}
					sb.append(" class=\"select\" "+(width==0?"":"style=\"width: "+width+"px\"") +"  >\n");
				}
				if(StringUtils.isEmpty(prompt)&& this.hasEmpty){
				  sb.append("<option value=\"\">请选择</option>");
				}
				else if (prompt != null) {
					sb.append("    <option value=\"\" >");
					sb.append(prompt);
					sb.append("</option>\n");
				}
				for (Iterator<LabelValue> i = codes.iterator(); i.hasNext();) {
					LabelValue labelValue = i.next();
					sb.append("<option value=\"" + labelValue.getValue() + "\"");

					if ((selected != null) && selected.trim().equals(labelValue.getValue().trim())) {
						sb.append(" selected=\"selected\"");
					}else if(null!=selected){
						String [] seva = selected.split(",");
						for (int j = 0; j < seva.length; j++) {
							String lava = labelValue.getValue().trim();
							String sevalue  = seva[j].trim();
							if(sevalue.equals(lava)){
								sb.append(" selected=\"selected\"");
								break;
							}
						}
					}
					sb.append(">" + labelValue.getLabel() + "</option>\n");
				}
				sb.append("</select>");
				try {
					pageContext.getOut().write(sb.toString());
				} catch (IOException io) {
					throw new JspException(io);
				}
			}

		}

		return super.doStartTag();
	}
	

	/**
	 * Release aquired resources to enable tag reusage.
	 * 
	 * @see javax.servlet.jsp.tagext.Tag#release()
	 */
	public void release() {
		super.release();
	}


	public void setWidth(int width) {
		this.width = width;
	}

}
